package com.example.vue.comm;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.vue.comm.util.WebUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 公共接口
 *
 * @author zhouxl
 * @DATE 2020/11/27
 * @descriptoin
 */
@Controller
@RequestMapping("api/comm")
public class CommAction {

    /**
     * 获取验证码
     *
     * @return
     */
    @RequestMapping("/code")
    public String getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/png");
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 20);
        String code = lineCaptcha.getCode();
        lineCaptcha.write(response.getOutputStream());
        ServletContext servletContext = request.getServletContext();
        String ip = WebUtil.getIpAddr(request);
        servletContext.setAttribute(ip, code);
        return null;
    }

}
